﻿#region 头注释
/******************************************************************************
文件名称    :   Hospitalization.cs
创建日期    :   2011/5/11 22:13:54
功能描述    :   Hospitalization实体类
修改日志    :   
    2011-05-11 22:13  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// Hospitalization实体类
    /// </summary>
    public class Hospitalization : EntityBase<Hospitalization, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Hospitalization()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            date = DateTime.MinValue;
            patient = null;
            patientId = null;
            chemistry = null;
            chemistryId = null;
            optics = null;
            opticsId = null;
            surgery = null;
            surgeryId = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override Hospitalization CreateNew()
        {
            return new Hospitalization();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.Hospitalization; }
        }

        /// <summary>
        /// 获取或设置Date
        /// </summary>
        public DateTime Date
        {
            get { return date; }
            set
            {
                if (entityState == EntityState.Saved && date != value) entityState = EntityState.Changed;
                date = value;
            }
        }
        protected DateTime date;

        /// <summary>
        /// 获取或设置PatientId
        /// </summary>
        public Patient Patient
        {
            get { return patient; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != patientId) entityState = EntityState.Changed;
                patient = value;
            }
        }
        protected Patient patient;

        /// <summary>
        /// 获取或设置PatientId
        /// </summary>
        public long? PatientId
        {
            get { return patient == null ? patientId : patient.Id; }
            set { patientId = value; }
        }
        protected long? patientId;

        /// <summary>
        /// 获取或设置化验检查
        /// </summary>
        public Chemistry Chemistry
        {
            get { return chemistry; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryId) entityState = EntityState.Changed;
                chemistry = value;
            }
        }
        protected Chemistry chemistry;

        /// <summary>
        /// 获取或设置化验检查ID
        /// </summary>
        public long? ChemistryId
        {
            get { return chemistry == null ? chemistryId : chemistry.Id; }
            set { chemistryId = value; }
        }
        protected long? chemistryId;

        /// <summary>
        /// 获取或设置光学诊断
        /// </summary>
        public Optics Optics
        {
            get { return optics; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != opticsId) entityState = EntityState.Changed;
                optics = value;
            }
        }
        protected Optics optics;

        /// <summary>
        /// 获取或设置光学诊断ID
        /// </summary>
        public long? OpticsId
        {
            get { return optics == null ? opticsId : optics.Id; }
            set { opticsId = value; }
        }
        protected long? opticsId;

        /// <summary>
        /// 获取或设置手术信息
        /// </summary>
        public Surgery Surgery
        {
            get { return surgery; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != surgeryId) entityState = EntityState.Changed;
                surgery = value;
            }
        }
        protected Surgery surgery;

        /// <summary>
        /// 获取或设置手术信息ID
        /// </summary>
        public long? SurgeryId
        {
            get { return surgery == null ? surgeryId : surgery.Id; }
            set { surgeryId = value; }
        }
        protected long? surgeryId;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override Hospitalization CopyTo(Hospitalization entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.date = this.date;
            entity.patient = this.patient;
            entity.patientId = this.patientId;
            entity.chemistry = this.chemistry;
            entity.chemistryId = this.chemistryId;
            entity.optics = this.optics;
            entity.opticsId = this.opticsId;
            entity.surgery = this.surgery;
            entity.surgeryId = this.surgeryId;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<Hospitalization>
        {
            Hospitalization entity;
            public DefaultXmlSerializer(Hospitalization entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public Hospitalization Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Date":
                        entity.date = XmlConvert.ToDateTime(reader.ReadElementString(), XmlDateTimeSerializationMode.Utc);
                        break;
                    case "PatientId":
                        entity.patientId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryId":
                        entity.chemistryId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "OpticsId":
                        entity.opticsId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "SurgeryId":
                        entity.surgeryId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                writer.WriteElementString("Date", XmlConvert.ToString(entity.Date, XmlDateTimeSerializationMode.Utc));
                if (entity.PatientId != null) writer.WriteElementString("PatientId", XmlConvert.ToString(entity.PatientId.Value));
                if (entity.ChemistryId != null) writer.WriteElementString("ChemistryId", XmlConvert.ToString(entity.ChemistryId.Value));
                if (entity.OpticsId != null) writer.WriteElementString("OpticsId", XmlConvert.ToString(entity.OpticsId.Value));
                if (entity.SurgeryId != null) writer.WriteElementString("SurgeryId", XmlConvert.ToString(entity.SurgeryId.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<Hospitalization>
        {
            Hospitalization entity;
            public DefaultJsonSerializer(Hospitalization entity)
                : base()
            {
                this.entity = entity;
            }

            public Hospitalization Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "date":
                        entity.date = DbConvert.ToNullableDateTime(propValue) ?? entity.date;
                        break;
                    case "patientId":
                        entity.patientId = DbConvert.ToNullableInt64(propValue) ?? entity.patientId;
                        break;
                    case "chemistryId":
                        entity.chemistryId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryId;
                        break;
                    case "opticsId":
                        entity.opticsId = DbConvert.ToNullableInt64(propValue) ?? entity.opticsId;
                        break;
                    case "surgeryId":
                        entity.surgeryId = DbConvert.ToNullableInt64(propValue) ?? entity.surgeryId;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //Date
                writer.WritePropertyName("date");
                writer.WriteValue(entity.Date);

                //PatientId
                writer.WritePropertyName("patientId");
                if (entity.PatientId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.PatientId.Value);
                }

                //化验检查ID
                writer.WritePropertyName("chemistryId");
                if (entity.ChemistryId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryId.Value);
                }

                //光学诊断ID
                writer.WritePropertyName("opticsId");
                if (entity.OpticsId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.OpticsId.Value);
                }

                //手术信息ID
                writer.WritePropertyName("surgeryId");
                if (entity.SurgeryId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.SurgeryId.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}